package com.bitmain.mmst.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bitmain.mmst.core.struct.APIResult;
import com.bitmain.mmst.domain.MinerState;
import com.bitmain.mmst.feature.cache.GlobalDataBuffer;
import com.bitmain.mmst.feature.time.TimeService;
import com.bitmain.ulnar.aider.DateAider;

/**
 *
 * @author Richard - rui.wu@bitmain.com
 * @since 2016年8月25日
 * 
*/

@Controller
@RequestMapping("miner")
public class MinerStateController {
	
	@RequestMapping(value = "state", method = RequestMethod.GET)
	public ModelAndView state(){
		ModelAndView mv = new ModelAndView("miner/state");
		return mv;
	}
	
	/** 状态缓存最近更新时间 */
	@RequestMapping(value = "states/time", method = RequestMethod.GET)
	@ResponseBody
	public APIResult cachedTime(HttpServletRequest request) {
		APIResult result = new APIResult();
		long cachedTime = TimeService.getTimeMillis();
		String time = DateAider.formatMillis(cachedTime, TimeService.getTimeZone());
		time += " (" + TimeService.getTimeZone().getDisplayName(request.getLocale()) + ")";
		result.setSuccess(true);
		result.setData(time);
		return result;
	}
	
	/** 所有矿机状态 */
	@RequestMapping(value = "states/miners", method = RequestMethod.GET)
	@ResponseBody
	public APIResult miners(){
		APIResult result = new APIResult();
		Map<String, MinerState> allStates = GlobalDataBuffer.getMinerStates();
		result.setData(allStates.values());
		result.setSuccess(true);
		return result;
	}
}

